﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IVisual._SC
{
    /// <summary>
    /// Allgemeine statische Tools
    /// </summary>
    public class cls_tools
    {
        /// <summary>
        /// Gibt die loakle IP-Adresse zurück
        /// </summary>
        /// <returns></returns>
        public static string LocalIPAddress()
        {
            IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "start");

            System.Net.IPHostEntry host;
            string localIP = "";
            host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
            foreach (System.Net.IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }

            IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.INFO, "Lokale IP: " + localIP);
            IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
            return localIP;
        }

        /// <summary>
        /// Versucht das Auflösen eines Hostnamens
        /// </summary>
        /// <param name="hostname">name des aufzulösendes Hosts</param>
        /// <returns>Gibt die IP zurück</returns>
        public static System.Net.IPAddress try_resolve(string hostname)
        {
            IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "start");
            if (hostname == "")
                return System.Net.IPAddress.Parse("0.0.0.0");

            try
            {
                System.Net.IPAddress _tmp;
                if (System.Net.IPAddress.TryParse(hostname, out _tmp))
                    return _tmp;

                System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(hostname);

                if (hostEntry.AddressList.Length > 0)
                {
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.INFO, "IP zu " + hostname + " ist: " + hostEntry.AddressList[0].ToString());
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
                    return hostEntry.AddressList[0];
                }
                else
                {
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.INFO, "Keine IP zu " + hostname + " gefunden. Versuche IP zu Parsen....");
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
                    return System.Net.IPAddress.Parse(hostname);
                }
            }
            catch (Exception ex)
            {
                IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.ERROR, ex.Message);
                IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
                return System.Net.IPAddress.Parse("0.0.0.0");
            }
        }

        /// <summary>
        /// Versucht aus einer IP den hostnamen aufzulösen
        /// </summary>
        /// <param name="IpAddressString">IP</param>
        /// <returns>hostname oder die IP selbst, wenn nicht auflösbar</returns>
        public static string try_reverse_resolve(string IpAddressString)
        {
            IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "start");

            try
            {
                System.Net.IPAddress hostIPAddress = System.Net.IPAddress.Parse(IpAddressString);
                System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostEntry(hostIPAddress);
                // System.Net.IPAddress[] address = hostInfo.AddressList;
                String[] alias = hostInfo.Aliases;

                if (alias.Length > 0)
                {
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.INFO, "Alias für " + IpAddressString + " ist " + alias[0]);
                    IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
                    return alias[0];
                }

                IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");
                return IpAddressString;
            }
            catch (Exception ex)
            {
                IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.ERROR, ex.Message);
                IVisual._SC.cls_LOG.WRITE_LOG_LINE(IVisual._SC.cls_LOG.LOGLEVEL.METHOD_INVOKE, "ende");

                return IpAddressString;
            }
        }
    }
}
